package com.qing.aclservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qing.aclservice.entity.pojo.AclPermission;
import com.qing.aclservice.entity.pojo.AclRolePermission;
import com.qing.aclservice.helper.PermissionHelper;
import com.qing.aclservice.mapper.AclPermissionMapper;
import com.qing.aclservice.service.AclPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.aclservice.service.AclRolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static com.qing.aclservice.helper.PermissionHelper.bulid;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author Yjx
 * @since 2022-09-19
 */
@Service
public class AclPermissionServiceImpl extends ServiceImpl<AclPermissionMapper, AclPermission> implements AclPermissionService {

    private final AclPermissionMapper aclPermissionMapper;
    private final AclRolePermissionService aclRolePermissionService;


    public AclPermissionServiceImpl(AclPermissionMapper aclPermissionMapper,
                                    AclRolePermissionService aclRolePermissionService) {
        this.aclPermissionMapper = aclPermissionMapper;
        this.aclRolePermissionService = aclRolePermissionService;
    }

    @Override
    public List<AclPermission> queryAllMenu() {
        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        List<AclPermission> permissionList = baseMapper.selectList(wrapper);
        List<AclPermission> result = PermissionHelper.bulid(permissionList);
        return result;
    }

    @Override
    public void removeChildById(String id) {
        List<String> idList = new ArrayList<>();
        this.selectChildListById(id, idList);
//把根据节点id放到list中
        idList.add(id);
        baseMapper.deleteBatchIds(idList);
    }

    @Override
    public void selectChildListById(String id, List<String> idList) {
        List<AclPermission> childList = baseMapper.selectList(new
                QueryWrapper<AclPermission>().eq("pid", id).select("id"));
        childList.stream().forEach(item -> {
            idList.add(item.getId());
            this.selectChildListById(item.getId(), idList);
        });
    }

    @Override
    public void saveRolePermissionRealtionShip(String roleId, String[] permissionIds) {
        aclRolePermissionService.remove(new QueryWrapper<AclRolePermission>().eq("role_id",
                roleId));
        List<AclRolePermission> rolePermissionList = new ArrayList<>();
        for(String permissionId : permissionIds) {
            if(StringUtils.isEmpty(permissionId)) continue;
            AclRolePermission rolePermission = new AclRolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermissionList.add(rolePermission);
        }
        aclRolePermissionService.saveBatch(rolePermissionList);
    }
}
